package com.xindaoapp.happypet.ui;

import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.xindaoapp.happypet.R;

public class NoAnimationActivity extends UMBaseActivity {

    /** 播放加载时动画 */
    protected FrameLayout loading;
    protected ImageView imageView;
    protected AnimationDrawable animation;
    protected View load_fail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getContentViewResId() != 0) {
            setContentView(getContentViewResId());
        }

        initViews();
        initEvents();

        onLoadDatas();
    }

    protected int getContentViewResId() {
        return 0;
    }

    protected void initViews() {

    }

    protected void initEvents() {}

    protected void onLoadDatas() {
        loading = (FrameLayout) findViewById(R.id.loading);
        load_fail = findViewById(R.id.ll_load_fail);

        imageView = (ImageView) findViewById(R.id.imageView);
        if (imageView != null) {
            imageView.setBackgroundResource(R.anim.alertdialog_animation);
            animation = (AnimationDrawable) imageView.getBackground();
            animation.setOneShot(false);
            // animation.start();
            startAnimation();
        }
        loadDatas();
    }

    protected void loadDatas() {}


    public void startAnimation() {
        if (loading.getVisibility() == View.GONE) {
            loading.setVisibility(View.VISIBLE);
        }
        if (load_fail.getVisibility() == View.VISIBLE) {
            load_fail.setVisibility(View.GONE);
        }
        if (animation != null && null != imageView) {
            animation.start();
        }
    }

    protected final void onDataArrived(boolean isArrived) {
        if (isArrived) {
            if (animation != null && null != imageView) {
                animation.stop();
                if (load_fail.getVisibility() == View.VISIBLE) {
                    load_fail.setVisibility(View.GONE);
                }
                loading.setVisibility(View.GONE);
            }
        } else {
            if (animation != null && null != imageView) {
                animation.stop();
                if (loading.getVisibility() == View.GONE) {
                    loading.setVisibility(View.VISIBLE);
                }
                if (load_fail.getVisibility() == View.GONE) {
                    load_fail.setVisibility(View.VISIBLE);
                }
            }
        }
    }

}
